{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "import demjson\n",
    "from jsonpath import jsonpath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(year,month):\n",
    "    url = 'http://api.tianapi.com/txapi/jiejiari/index?key=api密钥&date=' + year + '-' + month + '&mode=1&type=2'\n",
    "    res = requests.get(url)\n",
    "    json_data = demjson.decode(res.text)\n",
    "    date_list = jsonpath(json_data, '$..date')\n",
    "    info_list = jsonpath(json_data, '$..info')\n",
    "    cnweekday_list = jsonpath(json_data, '$..cnweekday')\n",
    "    name_list = jsonpath(json_data, '$..name')\n",
    "    isnotwork_list = jsonpath(json_data, '$..isnotwork')\n",
    "    vacation_list = jsonpath(json_data, '$..vacation')\n",
    "    remark_list = jsonpath(json_data, '$..remark')\n",
    "    tip_list = jsonpath(json_data, '$..tip')\n",
    "    list_content = [date_list,info_list,cnweekday_list,name_list,isnotwork_list,vacation_list,remark_list,tip_list]\n",
    "    df_jsonpath = pd.DataFrame(list_content)\n",
    "    df_jsonpath = df_jsonpath.T\n",
    "    df_jsonpath.rename(columns={\n",
    "                                0:'公历日期',\n",
    "                                1:'日期类型',\n",
    "                                2:'公历星期',\n",
    "                                3:'节假日名称',\n",
    "                                4:'是否需要上班',\n",
    "                                5:'节假日数组',\n",
    "                                6:'调休日数组',\n",
    "                                7:'放假提示',\n",
    "                                },inplace=True)\n",
    "    df_jsonpath.sort_values(by=['公历日期'],ascending=True,inplace=True)\n",
    "    num_to_str = { 0 : '工作日', \n",
    "                   1 : '休息日', \n",
    "                  }\n",
    "    df_jsonpath['是否需要上班'] = df_jsonpath['是否需要上班'].map(num_to_str)\n",
    "    return df_jsonpath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          公历日期 日期类型 公历星期     节假日名称 是否需要上班  \\\n",
      "0   2021-01-01  节假日  星期五       元旦节    休息日   \n",
      "1   2021-01-02  节假日  星期六       元旦节    休息日   \n",
      "2   2021-01-03  节假日  星期日       元旦节    休息日   \n",
      "3   2021-01-04  工作日  星期一              工作日   \n",
      "4   2021-01-05  工作日  星期二              工作日   \n",
      "..         ...  ...  ...       ...    ...   \n",
      "26  2021-12-27  工作日  星期一              工作日   \n",
      "27  2021-12-28  工作日  星期二              工作日   \n",
      "28  2021-12-29   节日  星期三  国际生物多样性日    工作日   \n",
      "29  2021-12-30  工作日  星期四              工作日   \n",
      "30  2021-12-31  工作日  星期五              工作日   \n",
      "\n",
      "                                   节假日数组 调休日数组         放假提示  \n",
      "0   [2021-01-01, 2021-01-02, 2021-01-03]        1月1日放假，共3天。  \n",
      "1   [2021-01-01, 2021-01-02, 2021-01-03]        1月1日放假，共3天。  \n",
      "2   [2021-01-01, 2021-01-02, 2021-01-03]        1月1日放假，共3天。  \n",
      "3                                                            \n",
      "4                                                            \n",
      "..                                   ...   ...          ...  \n",
      "26                                                           \n",
      "27                                                           \n",
      "28                                                           \n",
      "29                                                           \n",
      "30                                                           \n",
      "\n",
      "[365 rows x 8 columns]\n"
     ]
    }
   ],
   "source": [
    "year = '2021'\n",
    "months = [str(i).rjust(2,'0') for i in range(1,13)]\n",
    "data = pd.DataFrame()\n",
    "for month in months:\n",
    "    df = get_data(year,month)\n",
    "    data = data.append(df)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成 ./2021年节假日（自用版）.xlsx 成功！！\n"
     ]
    }
   ],
   "source": [
    "filename = f'./{year}年节假日（自用版）.xlsx'\n",
    "data.to_excel(filename,index=False)\n",
    "print(f'生成 {filename} 成功！！')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
